const ENV = "dev";
const DEV_BASE_URL = `http://localhost:8080`;
const SIT_BASE_URL = `http://localhost:8080`;
const BASE_URL = ENV == "dev" ? DEV_BASE_URL : SIT_BASE_URL;

const request = (options) =>
  new Promise((resole, reject) => {
    uni.request({
      url: BASE_URL + options.url,
      method: options.method || "GET",
      header: {
        token: uni.getStorageSync("token") ? uni.getStorageSync("token") : "",
      },
      data: options.data || {},
      success(res) {
        if (res.data) {
          if (res.data.code) {
            if (res.data.code === "401") {
              uni.navigateTo({
                url: "/pages/login/login",
              });
              return;
            } else if (res.data.code !== "200") {
              uni.showToast({
                icon: "error",
                title: "操作错误",
              });
            }
            resolve(res.data);
          }
        }
      },
      fail(error) {
        console.log(error);
        uni.showToast({
          icon: "error",
          title: "系统错误",
        });
      },
    });
  });

export default request;
